
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( 'shared.lua' )

function ENT:Initialize()

	self.HP = GameInfo.BeaconHealth or 500
	self:SetModel("models/props_c17/canister_propane01a.mdl")
	local color = self:GetTeamColor()
	self:SetColor( color.r,color.g,color.b,color.a )
	self:SetName("beacon"..self:GetTeam())
	
	local tracedata = {}
	tracedata.start = self:GetPos()
	tracedata.endpos = self:GetPos() + Vector(0,0,-900)
	tracedata.filter = self
	local trace = util.TraceLine(tracedata)
	self:SetPos(trace.HitPos)
	
	self:PhysicsInit( SOLID_VPHYSICS )
	self:SetMoveType( MOVETYPE_NONE ) //freeze, bitch
	self:SetSolid( SOLID_VPHYSICS )
	
	local laser = ents.Create( "env_laser" )
	
	laser:SetPos( self:GetPos() + (self:GetForward() * -5) )
	laser:SetParent( self )

	laser:SetKeyValue( "LaserTarget", "beacon"..self:GetTeam() )
	laser:SetKeyValue( "renderamt", "120" )
	laser:SetKeyValue( "width" , "1" )
	laser:SetKeyValue( "texture", "sprites/laserbeam.spr" )
	laser:SetKeyValue( "TextureScroll" , "2" )
	laser:SetKeyValue( "damage", "0" )
	laser:SetKeyValue( "NoiseAmplitude", "20" )
	laser:SetKeyValue( "dissolvetype", "None" )
	laser:SetKeyValue( "rendermode", "5" )
	laser:SetKeyValue( "spawnflags", "1" )
	laser:SetKeyValue( "rendercolor", ""..color.r.." "..color.g.." "..color.b.."" )
	laser:SetName("laz0r")
	laser:SetPos(self:GetPos()+Vector(0,0,150))
	laser:Spawn()
	self.Laser = laser
	
end

function ENT:Think()
	local color
	if self:GetTeam() == 1 then
		color = Angle(255,0,0)
	else
		color = Angle(0,0,255)
	end
	local effectdata = EffectData()
	effectdata:SetOrigin(self:GetPos() + Vector(0,0,150))
	effectdata:SetAngle(color)
	util.Effect("beacon_topflare", effectdata)
end

function ENT:OnTakeDamage(dmg)
	if dmg:GetAttacker() and !dmg:IsExplosionDamage() then
		if dmg:GetDamage() == 0 then return end
		if dmg:GetAttacker():Team() == self:GetTeam() then return end
		self:SetHP(self:GetHP() - dmg:GetDamage())
		local rand = math.random(1,4)
		if rand == 4 then
			self:EmitSound(Sound(ChooseRandom(ElectricBoom)),140,math.random(100,110))
			local effectdata = EffectData()
			effectdata:SetOrigin(self:GetPos())
			effectdata:SetAngle(self:GetAngles())
			util.Effect("prop_gib", effectdata)
		end
		for k,v in pairs(player.GetAll()) do
			v:SetNetworkedInt("beaconhealth"..self:GetTeam(),self:GetHP())
		end
	end
	if self:GetHP() <= 0 then
	
		if dmg:GetAttacker():IsPlayer() then
			dmg:GetAttacker():AddFrags(5)
		end
	
		self:EmitSound(Sound(ChooseRandom(BombExplosion)),100,math.random(100,110))
		self.Laser:Remove()
		
		local color
		if self:GetTeam() == 1 then
			color = Angle(255,0,0)
		else
			color = Angle(0,0,255)
		end
		local effectdata = EffectData()
		effectdata:SetOrigin(self:GetPos() + Vector(0,0,150))
		effectdata:SetAngle(color)
		util.Effect("beacon_flaredeath", effectdata)
		
		for i=1,math.random(10,20) do
			local effectdata = EffectData()
			effectdata:SetOrigin(self:GetPos())
			util.Effect("prop_gib", effectdata)
		end
		
		if self:GetTeam() == 1 then
			color = Angle(255,200,200)
		else
			color = Angle(200,200,255)
		end
		local effectdata = EffectData()
		effectdata:SetOrigin(self:GetPos())
		effectdata:SetAngle(color)
		util.Effect("beacon_explosion", effectdata)
		
		GAMEMODE:EndRound(self:GetTeam())
	end
end 

function ENT:KeyValue( key, value )
	if ( key == "team" ) or ( key == "Team" ) then
		self:SetTeam( tonumber(value) )
	end
end 

function ENT:SetHP(v)
	self.HP = v
end

function ENT:SetupHP()
	self.HP = GameInfo.BeaconHealth or 500
end

function ENT:GetHP(team)
return self.HP end

function ENT:SetTeam(team)
	self.Team = team
end

function ENT:GetTeamColor()
	if self.Team == 1 then
		return Color(255,10,10,255)
	else
		return Color(10,10,255,255)
	end
end

//use this to determine the team
function ENT:GetTeam()
return self.Team or 0 end